package com.yanggeng.demo.beginnerguidedemo.utils;

import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;


import com.yanggeng.demo.beginnerguidedemo.R;

import java.util.Calendar;

/**
 * 提示对话框工具类
 * Created by yanggeng on 2017/6/28.
 */

public class NoticeDialog {

    private static AlertDialog alertDialog;

    /**
     * 最简化确认提示对话框调用
     *
     * @param context
     * @param message
     */
    public static void alert(Context context, String message) {
        alert(context, message, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
    }

    /**
     * 显示确认提示对话框
     *
     * @param context
     * @param message
     * @param positiveButtonListener
     */
    public static void alert(Context context, String message, DialogInterface.OnClickListener positiveButtonListener) {
        alert(context, "提示", message, "确认", positiveButtonListener);
    }

    /**
     * 显示确认提示对话框
     *
     * @param context
     * @param title
     * @param message
     * @param positiveButtonText
     * @param positiveButtonListener
     */
    public static void alert(Context context, String title, String message, String positiveButtonText,
                             DialogInterface.OnClickListener positiveButtonListener) {

        if (alertDialog != null && alertDialog.isShowing()) {
            return;
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(positiveButtonText, positiveButtonListener)
                    .setCancelable(false);
            alertDialog = builder.create();
            alertDialog.show();
//            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.c_login_button));
        }

    }

    /**
     * 显示确认or取消提示对话框
     *
     * @param context
     * @param message
     * @param positiveButtonListener
     * @param negativeButtonListener
     */
    public static void confirm(Context context, String message, DialogInterface.OnClickListener positiveButtonListener, DialogInterface.OnClickListener negativeButtonListener) {
        confirm(context, "提示", message, "确定", positiveButtonListener, "取消", negativeButtonListener);
    }

    /**
     * 显示确认提示对话框
     *
     * @param context
     * @param title
     * @param message
     * @param positiveButtonText
     * @param positiveButtonListener
     * @param negativeButtonText
     * @param negativeButtonListener
     */
    public static void confirm(Context context, String title, String message, String positiveButtonText,
                               DialogInterface.OnClickListener positiveButtonListener,
                               String negativeButtonText,
                               DialogInterface.OnClickListener negativeButtonListener) {
        confirm(context, title, message, positiveButtonText, positiveButtonListener, -1, negativeButtonText, negativeButtonListener, -1);
    }

    /**
     * 三个按钮的确认对话框
     *
     * @param context
     * @param title
     * @param message
     * @param positiveButtonText
     * @param positiveButtonListener
     * @param negativeButtonText
     * @param negativeButtonListener
     * @param neutralButtonText
     * @param neutralButtonListener
     */
    public static void confirm(Context context, String title, String message, String positiveButtonText,
                               DialogInterface.OnClickListener positiveButtonListener,
                               String negativeButtonText,
                               DialogInterface.OnClickListener negativeButtonListener,
                               String neutralButtonText,
                               DialogInterface.OnClickListener neutralButtonListener) {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.setTitle(title);
            alertDialog.setMessage(message);
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, positiveButtonText, positiveButtonListener);
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, negativeButtonText, negativeButtonListener);
            alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, neutralButtonText, neutralButtonListener);
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(positiveButtonText, positiveButtonListener)
                    .setNegativeButton(negativeButtonText, negativeButtonListener)
                    .setNeutralButton(neutralButtonText, neutralButtonListener);
            alertDialog = builder.create();
            alertDialog.show();
//            alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.c_login_button));
//            alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.c_text_gray));
//            alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setTextColor(context.getResources().getColor(R.color.c_text_gray));
        }
    }

    public static void confirm(Context context, String title, String message, String positiveButtonText,
                               DialogInterface.OnClickListener positiveButtonListener,
                               int positiveButtonTextColor,
                               String negativeButtonText,
                               DialogInterface.OnClickListener negativeButtonListener,
                               int negativeButtonTextColor) {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.setTitle(title);
            alertDialog.setMessage(message);
            alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, positiveButtonText, positiveButtonListener);
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, negativeButtonText, negativeButtonListener);
            if (positiveButtonTextColor != -1) {
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(positiveButtonTextColor);
            }
            if (negativeButtonTextColor != -1) {
                alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(negativeButtonTextColor);
            }
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle(title)
                    .setMessage(message)
                    .setPositiveButton(positiveButtonText, positiveButtonListener)
                    .setNegativeButton(negativeButtonText, negativeButtonListener);
            alertDialog = builder.create();
            alertDialog.show();
            if (positiveButtonTextColor != -1) {
                alertDialog.getButton(DialogInterface.BUTTON_POSITIVE).setTextColor(positiveButtonTextColor);
            } else {
//                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(context.getResources().getColor(R.color.c_login_button));
            }
            if (negativeButtonTextColor != -1) {
                alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setTextColor(negativeButtonTextColor);
            } else {
//                alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE).setTextColor(context.getResources().getColor(R.color.c_text_gray));
            }
        }
    }

    public static void confirmRedAndBlue(Context context, String title, String message, String positiveButtonText,
                                         DialogInterface.OnClickListener positiveButtonListener,
                                         String negativeButtonText,
                                         DialogInterface.OnClickListener negativeButtonListener) {
        confirm(context, title, message, positiveButtonText, positiveButtonListener, Color.parseColor("#EB303A"), negativeButtonText, negativeButtonListener, Color.parseColor("#007AFF"));
    }

    public static void showDatePicker(Context context, View view, DatePickerDialog.OnDateSetListener onDateSetListener) {
        // 强制隐藏键盘
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, onDateSetListener, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    public static void showListDialog(Context context, View contentView,  String title, String[] data, OnItemClickListener onItemClickListener) {
        // 强制隐藏键盘
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(contentView.getWindowToken(), 0);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View view = LayoutInflater.from(context).inflate(R.layout.layout_recycleview, null);
        RecyclerView rv = (RecyclerView) view.findViewById(R.id.recycle_view);
        rv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        ListAdapter adapter = new ListAdapter(context, data, title, onItemClickListener);
        rv.setAdapter(adapter);
        rv.addItemDecoration(new MyDecoration(context));
        builder.setView(view)
                .setCancelable(true);
        alertDialog = builder.create();
        alertDialog.show();
    }

    static class ListAdapter extends RecyclerView.Adapter<ListAdapter.TestViewHolder> {
        String[] datas;
        LayoutInflater inflater;
        String title;
        OnItemClickListener onItemClickListener;

        public ListAdapter(Context context, String[] data, String title, OnItemClickListener onItemClickListener) {
            this.datas = data;
            inflater = LayoutInflater.from(context);
            this.title = title;
            this.onItemClickListener = onItemClickListener;
        }

        @Override
        public TestViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new TestViewHolder(inflater.inflate(R.layout.layout_simple_item, parent, false));
        }

        @Override
        public void onBindViewHolder(final TestViewHolder holder, final int position) {
            if (position == 0) {
                holder.button.setText(title);
                holder.button.setClickable(false);
            } else {
                holder.button.setText(datas[position - 1]);
                holder.button.setClickable(true);
                holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemClickListener.onItemClick(v, position - 1);
                        if (alertDialog != null && alertDialog.isShowing()) {
                            alertDialog.dismiss();
                        }
                    }
                });
            }
        }

        @Override
        public int getItemCount() {
            return datas.length + 1;
        }

        public void notifyData(String[] data) {
            this.datas = data;
            notifyDataSetChanged();
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }

        class TestViewHolder extends RecyclerView.ViewHolder {

            Button button;

            public TestViewHolder(View itemView) {
                super(itemView);
                button = (Button) itemView.findViewById(R.id.btn_province);
            }
        }

    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }
}
